import {defineStore} from 'pinia'
import {IOptionItem, IPage} from "../domains/qy-component.ts";
import {createComponent} from "../components/tool.ts";

export interface IComState {
    // 模板信息
    page: IPage,
    optionItems: IOptionItem[]
}

export const useComStore = defineStore('com', {
    state: (): IComState => ({
        // 模板信息
        page: {},
        optionItems: [], // 可选项
    }),
    getters: {},
    actions: {
        initPage(pageInfo: IPage) {
            this.page = pageInfo
        },
        initOptionItems(options: IOptionItem) {
            this.optionItems = options
        },
        async add(index, x, y) {
            let com = await createComponent(this.optionItems[index].type)
            com.attr.x = x;
            com.attr.y = y;
            this.page.tempItems.push({
                ...com,
                ...this.optionItems[index]
            })
        }
    }
})
